import json,os
from tasks.joinPage import saveVideo
from tasks.init_selenium import Feigua
from tasks.spider_task_cookie import save_cookie, get_data
from deal_url.topTwenty_url import top_20_url, detail_url
import requests
from config.local_config import ROOT_PATH
from tasks.convertTxt import splitVideo,dealResult
def main():
    try:
        fei = Feigua()
        # 拿到cookie
        save_cookie(fei)
        # 生成含有20条数据的url
        list = []
        for index in range(1, 3):
            url = top_20_url(index)
            # 拿到前20条数据
            json_data = get_data(url)
            list.append(json_data)
        # 需要的排版数据
        datailList = [result for data in list for result in detail_url(data)[1]]
        # 视频详情数据
        videoUrls = [videoUrl for data in list for videoUrl in detail_url(data)[0]]

        filePath = ROOT_PATH
        if not os.path.exists(filePath):
            os.makedirs(filePath)

        videoPath_list = []
        # 进入页面
        for index, videoUrl in enumerate(videoUrls):
            try:
                # 把视频保存到文件里面
                videoPath = saveVideo(filePath, index, videoUrl, fei)
                videoPath_list.append(videoPath)
            except FileNotFoundError:
                print("视频文件不存在")

        for path in videoPath_list:
            # 视频太长了，进行划分
            data_list = splitVideo(path)
            # 处理返回的视频文本
            dealResult(data_list)
    except FileNotFoundError:
        print("视频文件不存在")

if __name__ == '__main__':
    main()
